/************************************************************************************/
/*  Program name CAPGLIMP_JE_HEADERS.sql                                        	*/
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 24 JAN, 2006   NIP Karen         First created                                   */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPGLIMP_JE_HEADERS';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_GL_JE_HEADERS;
	
	lv_Next_Seq_ID		NUMBER;
	
BEGIN
	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	GL_JE_HEADERS_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT	INTO CRST_GL_JE_HEADERS_MAP	VALUES
			(
			C1.JE_HEADER_ID,
			C1.JE_BATCH_ID,
			C1.NAME,
			lv_Next_Seq_ID,
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.JE_BATCH_ID),
			C1.NAME
			);
			
			INSERT INTO GL_JE_HEADERS
			VALUES
			(
			lv_Next_Seq_ID,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_NAME),
			CAPBRIMP_COMMON.GetSOBID(C1.SOB_SHORT_NAME),
			C1.JE_CATEGORY,
			C1.JE_SOURCE,
			C1.PERIOD_NAME,
			C1.NAME,
			C1.CURRENCY_CODE,
			C1.STATUS,
			C1.DATE_CREATED,
			C1.ACCRUAL_REV_FLAG,
			C1.MULTI_BAL_SEG_FLAG,
			C1.ACTUAL_FLAG,
			C1.DEFAULT_EFFECTIVE_DATE,
			C1.TAX_STATUS_CODE,
			C1.CONVERSION_FLAG,
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_NAME),
			C1.LAST_UPDATE_LOGIN,
			CAPBRIMP_COMMON.GetEncumTypeID(C1.ENCUM_TYPE_NAME),
			C1.BUDGET_VERSION_ID,
			C1.BALANCED_JE_FLAG,
			C1.BALANCING_SEGMENT_VALUE,
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.JE_BATCH_ID),
			C1.FROM_RECURRING_HEADER_ID,
			C1.UNIQUE_DATE,
			C1.EARLIEST_POSTABLE_DATE,
			C1.POSTED_DATE,
			C1.ACCRUAL_REV_EFFECTIVE_DATE,
			C1.ACCRUAL_REV_PERIOD_NAME,
			C1.ACCRUAL_REV_STATUS,
			C1.ACCRUAL_REV_JE_HEADER_ID,
			C1.ACCRUAL_REV_CHANGE_SIGN_FLAG,
			C1.DESCRIPTION,
			C1.CONTROL_TOTAL,
			C1.RUNNING_TOTAL_DR,
			C1.RUNNING_TOTAL_CR,
			C1.RUNNING_TOTAL_ACCOUNTED_DR,
			C1.RUNNING_TOTAL_ACCOUNTED_CR,
			C1.CURRENCY_CONVERSION_RATE,
			C1.CURRENCY_CONVERSION_TYPE,
			C1.CURRENCY_CONVERSION_DATE,
			C1.EXTERNAL_REFERENCE,
			C1.PARENT_JE_HEADER_ID,
			C1.REVERSED_JE_HEADER_ID,
			C1.ORIGINATING_BAL_SEG_VALUE,
			C1.INTERCOMPANY_MODE,
			C1.DR_BAL_SEG_VALUE,
			C1.CR_BAL_SEG_VALUE,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE10,
			C1.CONTEXT,
			C1.GLOBAL_ATTRIBUTE_CATEGORY,
			C1.GLOBAL_ATTRIBUTE1,
			C1.GLOBAL_ATTRIBUTE2,
			C1.GLOBAL_ATTRIBUTE3,
			C1.GLOBAL_ATTRIBUTE4,
			C1.GLOBAL_ATTRIBUTE5,
			C1.GLOBAL_ATTRIBUTE6,
			C1.GLOBAL_ATTRIBUTE7,
			C1.GLOBAL_ATTRIBUTE8,
			C1.GLOBAL_ATTRIBUTE9,
			C1.GLOBAL_ATTRIBUTE10,
			C1.USSGL_TRANSACTION_CODE,
			C1.CONTEXT2,
			C1.DOC_SEQUENCE_ID,
			C1.DOC_SEQUENCE_VALUE,
			C1.JGZZ_RECON_CONTEXT,
			C1.JGZZ_RECON_REF,
			C1.REFERENCE_DATE,
			C1.LOCAL_DOC_SEQUENCE_ID,
			C1.LOCAL_DOC_SEQUENCE_VALUE
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	UPDATE
		GL_JE_HEADERS
	SET	
		PARENT_JE_HEADER_ID		=	CAPBRIMP_MAPPING.GetJEHeaderIDMapping(PARENT_JE_HEADER_ID),
		REVERSED_JE_HEADER_ID	=	CAPBRIMP_MAPPING.GetJEHeaderIDMapping(REVERSED_JE_HEADER_ID)
	WHERE
		PARENT_JE_HEADER_ID IS NOT NULL
	OR	REVERSED_JE_HEADER_ID IS NOT NULL
	AND	JE_HEADER_ID	IN
		(
		SELECT	JE_HEADER_ID
		FROM	CRST_GL_JE_HEADERS
		);
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;
	
	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/